package com.web.admin.web.base.user;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.core.facade.AreaFacade;
import com.web.base.join.AreaJoinHandler;
import com.web.base.user.facade.AddressFacade;
import com.web.base.user.facade.UserFacade;
import com.web.base.user.message.AddressRequest;
import com.web.base.user.message.AddressResponse;
import com.web.base.user.message.UserResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.validation.ValidationException;
import java.util.List;

/**
 * 收货地址控制器
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年06月26日
 */
@Controller("userAddressController")
@Http(url = "/base/user")
public class AddressController
{
	@Autowired
	private AddressFacade addressFacade;
	@Autowired
	private UserFacade userFacade;
	@Autowired
	private AreaFacade areaFacade;
	
	private static DictParser parser = DictParser.getInstance();
	
	public AddressController()
	{
	}

	@Http(url = "/address_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		int userId = request.getQueryInt("userId", 0);
		if (userId <= 0)
			throw new ValidationException("用户ID不能为空");

		UserResponse userResponse = userFacade.get(userId);
		List<AddressResponse> addressList = addressFacade.listByUser(userId, null);
		AreaJoinHandler.areaJoin(addressList, areaFacade);
		return View.createBuilder().setView("/base/user/address_list")
						.put("model", userResponse)
						.put("map", parser.getStringMap())
						.put("addressList", addressList)
						.build();
	}

    @Http(url = "/address_get", method = HttpMethod.GET)
    public View get(HttpRequest request)
    {
        int id = request.getQueryInt("id", 0);
        AddressResponse response = null;
        if (id > 0)
        {
            response = addressFacade.get(id);
            AreaJoinHandler.areaJoin(response, areaFacade);
        }
        return View.createBuilder().setView("/base/user/address_get")
                        .put("model", response == null ? new AddressResponse() : response)
                        .put("map", parser.getStringMap())
                        .build();
    }

    @Http(url = "/address_save", method = HttpMethod.POST)
    public void save(HttpRequest request)
    {
        AddressRequest addressRequest = request.getPostObject(AddressRequest.class);
        AddressResponse response = null;
        if (addressRequest.getId() == null)
        {
            ValidationUtil.valid(addressRequest, SaveModel.class);
            response = addressFacade.add(addressRequest);
        }
        else
        {
            ValidationUtil.valid(addressRequest, UpdateModel.class);
            response = addressFacade.update(addressRequest);
        }
    }

}
